package com.art.museumbackend.controller;

import com.art.museumbackend.entity.Columns;
import com.art.museumbackend.entity.Result;
import com.art.museumbackend.service.ColumnService;
import com.art.museumbackend.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/columns")
public class ColumnController {

    @Autowired
    private ColumnService columnService;

    @PostMapping("/addColumn")
    public Result addColumn(@RequestBody @Validated(Columns.Add.class)  Columns columns) {
        columnService.addColumn(columns);
        return ResultUtil.successMsg("栏目添加成功");
    }

    @GetMapping("/columnList")
    public Result<List<Columns>> listAllColumns() {
        List<Columns> columns = columnService.listAllColumns();
        return ResultUtil.success(columns);
    }

    @GetMapping("/getParentColumns")
    public Result<List<Columns>> getParentColumns() {
        List<Columns> columns=columnService.getParentColumns();
        return ResultUtil.success(columns);
    }

    @GetMapping("/getChildrenColumns")
    public Result<List<Columns>> getChildrenColumns(@RequestParam("pid") Integer pid) {
        List<Columns> columns=columnService.getChildrenColumns(pid);
        return ResultUtil.success(columns);
    }

    @GetMapping("/selectAllChildrenColumns")
    public Result<List<Columns>> selectAllChildrenColumns() {
        List<Columns> columns=columnService.selectAllChildrenColumns();
        return ResultUtil.success(columns);
    }



    @GetMapping("/columnDetail")
    public Result<Columns> getColumnById(@RequestParam("cid") Integer cid) {
        Columns columns = columnService.getColumnById(cid);
        return ResultUtil.success(columns);
    }

    @PutMapping("/updateColumn")
    public Result updateColumn(@RequestBody @Validated(Columns.Update.class) Columns columns) {
        columnService.updateColumn(columns);
        return ResultUtil.successMsg("栏目更新成功");
    }

    @DeleteMapping("/deleteColumn")
    public Result deleteColumn(@RequestParam("columnId") Integer columnId) {
        columnService.deleteColumn(columnId);
        return ResultUtil.successMsg("栏目删除成功");
    }
}
